<?php
    global $mikron;
    $mikron->Tools->SiteMenu->setCurrentPage('/');
    
    $success_format = '<div class="success_box">%s</div>';
    $error_format = '<div class="error_box">%s</div>';
    
    if(isset($_GET['ok']))
    {
        echo sprintf($success_format, 'Тендер успешно сохранен.<br />Посмотреть все <a title="Страница со списком открытых тендеров" href="'.$mikron->Tools->MakePath('?customer_tenderspage=opened').'">открытые</a> тендеры.');
        return;
    }

    /*
    $formDescription = $mikron->EntityManager->ShowFormForUsers(new TENDER(), null, false, false, null, false, null, null, 'create_tender',
        null, null, null, null, 1, null, 240, null, false, 64, 64, 8, 0, 4, null, false);  
    $cityFieldDescription = $formDescription->getField('city');
    $cityFieldDescription->addAjaxFillFromFile('country', '/action/tosmr/getcities/?country={1}&format=options');
    $mikron->EntityManager->CreateFormValidator($formDescription);
    return;
    */

    // city
    //  /action/tosmr/getcities/?country={1}&format=options

    $name = null;
    $country_id = null;
    $city_id = null;
    $description = null;
    $condition = null;
    $curator = null;
    
    $tenderID = null;
    if(isset($_GET['edtender']))
    {
        $tenderID = (int)$_GET['edtender'];
    }

    $f1 = new html_form('performer_info');
    $tender = new TENDER($tenderID);
    if($f1->pageindex == 2)
    {
        $name = $mikron->Tools->GetParam($f1->formdata, 'name', null);
        $country_id = (int)$mikron->Tools->GetParam($f1->formdata, 'country', null);
        $city_id = (int)$mikron->Tools->GetParam($f1->formdata, 'city', null);
        $description = $mikron->Tools->GetParam($f1->formdata, 'description', null);
        $condition = $mikron->Tools->GetParam($f1->formdata, 'condition', null);
        $curator = $mikron->Tools->GetParam($f1->formdata, 'curator', null);

        $tender->name->value = $name;
        $tender->country->value = $country_id;
        $tender->city->value = $city_id;
        $tender->description->value = $description;
        $tender->condition->value = $condition;
        $tender->curator->value = $curator;

        try
        {
            $tender->Save();
            $mikron->Tools->GoToPage($mikron->Tools->MakePath('?ok'));
        }
        catch(Exception $ex)
        {
            echo sprintf($error_format, 'Ошибка сохранения тендера, пожалуйста исправьте введенные данные и попробуйте еще раз.');
        }

        $f1->pageindex = 1;
    }

    if(is_object($tender->country->value))
    {
        $country_id = $tender->country->value->id;
    }
    if(is_object($tender->city->value))
    {
        $city_id = $tender->city->value->id;
    }
    $tab1 = new html_table(1, 'microwindow', null, 0, 2);
    $tab1->newcol('Название:', true, null, null, 'caption');
    $tab1->newcol();
        $f1->addInput(1, 'name', null, 64, null, false, $tender->name->value);

    $tab1->newcol('Регион:', true, null, null, 'caption');
    $tab1->newcol();
        $items1 = $mikron->Queries->Query('COUNTRY');
        $f1->addSelectControlFromEntity(1, 'country', null, $items1, $country_id, Array('fieldname'=>'city', 'sourcefile'=> '/action/tosmr/getcities/?country={1}&format=options'), 'Укажите страну');

    $tab1->newcol();
        $items2 = Array();
        $items2 = $mikron->Queries->Query('CITY', null, new Criterion('country', $country_id));
        $f1->addSelectControlFromEntity(1, 'city', null, $items2, $city_id, null, 'Укажите город');

    $tab1->newcol('Описание:', true, null, null, 'caption');
    $tab1->newcol();
        $f1->addTextBox(1, 'description', null, false, $tender->description->value);

    $tab1->newcol('Условия:', true, null, null, 'caption');
    $tab1->newcol();
        $f1->addTextBox(1, 'condition', null, false, $tender->condition->value);

    $tab1->newcol('Куратор:', true, null, null, 'caption');
    $tab1->newcol();
        $f1->addInput(1, 'curator', null, 64, null, false, $tender->curator->value);

    $tab1->newcol(null, false, 'left');
        $f1->addSubmit('save', 'Добавить тендер');                
    $tab1->end();
    $f1->end();

?>